<?php
include("include/config.inc.php");

$pageName = 'incomingRegister';
if($_SESSION['s_userType'] == 'User')
{
   $authorization = "NoAccess";
   foreach($selUsermenuArr as $value)
   {
     if(in_array($pageName, $value))
       {
         $authorization ="Access";
         break;
       }
       else 
       {
        $authorization = "NoAccess";    
       }
   }
   if($authorization == "NoAccess")
   {
     header("location:index.php");
   }
}

if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:index.php");
}
else
{
	$catTitleArray = array();
	$challan       = array();
	$incomeingMasterId       = "";
	$printBtn = 0;

	if((isset($_POST['insertBtn']) || isset($_POST['printBtn'])) && strlen($_POST['partyId']) > 0)
	{
	  if(isset($_POST['printBtn']))
	  {
	  	$printBtn = 1;
	  }

		 $partyId      = isset($_POST['partyId']) ? $_POST['partyId'] : 0;
		 $IncomingDate = $_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay'];
		 $qty          = (isset($_POST['qty'])) && ($_POST['qty'] != "") ? $_POST['qty'] : 0 ;
		 $challan      = (isset($_POST['challan'])) && ($_POST['challan'] != "") ? $_POST['challan'] : 0 ;
		 $weight       = (isset($_POST['weight'])) && ($_POST['weight'] != "") ? $_POST['weight'] : 0 ;
		 $processId1   = (isset($_POST['processId1'])) && ($_POST['processId1'] != "") ? $_POST['processId1'] : 0 ;
		 $processId2   = (isset($_POST['processId2'])) && ($_POST['processId2'] != "") ? $_POST['processId2'] : 0 ;
		 $processId3   = (isset($_POST['processId3'])) && ($_POST['processId3'] != "") ? $_POST['processId3'] : 0 ;
		 $material     = (isset($_POST['material'])) && ($_POST['material'] != "") ? $_POST['material'] : 0 ;
		 $hardne       = (isset($_POST['hardne'])) && ($_POST['hardne'] != "") ? $_POST['hardne'] : 0 ;
		 $col1         = (isset($_POST['col1'])) && ($_POST['col1'] != "") ? $_POST['col1'] : NULL ;
		 $col2         = (isset($_POST['col2'])) && ($_POST['col2'] != "") ? $_POST['col2'] : NULL ;
		 $col3         = (isset($_POST['col3'])) && ($_POST['col3'] != "") ? $_POST['col3'] : NULL ;
		 $col4         = (isset($_POST['col4'])) && ($_POST['col4'] != "") ? $_POST['col4'] : NULL ;
		 $col5         = (isset($_POST['col5'])) && ($_POST['col5'] != "") ? $_POST['col5'] : NULL ;
		 $col6         = (isset($_POST['col6'])) && ($_POST['col6'] != "") ? $_POST['col6'] : NULL ;
	
	  $insertQueryParty = "INSERT INTO incoming (partyId,IncomingDate,qty,challan,weight,processId1,processId2,processId3,materialId,hardne,
	                                              column1,column2,column3,column4,column5,column6)
	                        VALUES('".$partyId."',
	                       			  '".$IncomingDate."',
	                       			  '".$qty."',
	                       			  '".$challan."',
	                       			  '".$weight."',
	                       			  '".$processId1."',
	                       			  '".$processId2."',
	                       			  '".$processId3."',
	                       			  '".$material."',
	                       			  '".$hardne."',
	                       			  '".$col1."',
	                       			  '".$col2."',
	                       			  '".$col3."',
	                       			  '".$col4."',
	                       			  '".$col5."',
	                       			  '".$col6."')";
	  $insertQueryPartydResult = mysql_query($insertQueryParty);
	  $incomeingMasterId = mysql_insert_id();
	  if(!$insertQueryPartydResult)
	  {
	    die("Insert Query Not Inserted 1: ".mysql_error());
	  }
	  else
	  {
	  	if($processId3 > 0){
	  	  $noOfProcess = 3;
	  	}
	  	else if($processId2 > 0){
	  	  $noOfProcess = 2;
	  	}
	  	else if($processId1 > 0){
	  	  $noOfProcess = 1;
	  	}
	
	    $insertPlanning = "INSERT INTO planning (incomingId,partyId,quantity,weight,processId1,processId2,processId3,noOfProcess)
	                       VALUES (".$incomeingMasterId.",'".$partyId."','".$qty."','".$weight."','".$processId1."','".$processId2."','".$processId3."','".$noOfProcess."')";
	    $insertPlanningRes = mysql_query($insertPlanning);
	    
	    $loopCount = 0;                             
	    while($loopCount < count($_POST['columns1']))
	    {
	      $columns1  = ($_POST['columns1'][$loopCount] != '') ? $_POST['columns1'][$loopCount] : NULL;
	      $columns2  = ($_POST['columns2'][$loopCount] != '') ? $_POST['columns2'][$loopCount] : NULL;
	      $columns3  = ($_POST['columns3'][$loopCount] != '') ? $_POST['columns3'][$loopCount] : NULL;
	      $columns4  = ($_POST['columns4'][$loopCount] != '') ? $_POST['columns4'][$loopCount] : NULL;
	      $columns5  = ($_POST['columns5'][$loopCount] != '') ? $_POST['columns5'][$loopCount] : NULL;
	      $columns6  = ($_POST['columns6'][$loopCount] != '') ? $_POST['columns6'][$loopCount] : NULL;
	    
	      $insertDetail = "INSERT INTO incomingdetail(incomingId,columns1,columns2,columns3,columns4,columns5,columns6)
	                       VALUES(".$incomeingMasterId.",
	                        			 '".$columns1."',
	                        			 '".$columns2."',
	                        			 '".$columns3."',
	                        			 '".$columns4."',
	                               '".$columns5."',
	                               '".$columns6."')";
	      $insertDetailRes = mysql_query($insertDetail);
	      if(!$insertDetailRes)
	      {  
	        die("Insert Query Not Inserted 2: ".mysql_error());
	      }
	      $loopCount++;
	    }
	  }
	}
	
	if(isset($_GET['incomingId']) && isset($_GET['directOut']))
	{
		$updateInc = "UPDATE incoming SET status = 'outgoing' WHERE incomingId = ".$_GET['incomingId'];
		$updateIncRes = mysql_query($updateInc);
		if($updateIncRes)
		{
	  	$updatePlan = "UPDATE planning SET status = 'outgoing' WHERE incomingId = ".$_GET['incomingId'];
		  $updatePlanRes = mysql_query($updatePlan);
		  if($updatePlanRes)
		  {
		  	$selPartyId = "SELECT partyId FROM incoming WHERE incomingId = ".$_GET['incomingId'];
		  	$selPartyIdRes = mysql_query($selPartyId);
		  	if($row = mysql_fetch_array($selPartyIdRes))
		  	{
		  	  header("Location:outGoingEntry.php?partyId=".$row['partyId']."&unprocessincomingId=".$_GET['incomingId']);
		  	  exit();
		  	}
		  }
		}
	}
	
	
	//--------------------------------insert over------------
	$k = 0;
	 $select = "SELECT partyId,partyName
	              FROM party
	             ORDER BY partyName";
	$selectResult = mysql_query($select);
	while($row = mysql_fetch_array($selectResult))
	{
	  $catTitleArray['partyId'][$k]   = $row['partyId'];
	  $catTitleArray['partyName'][$k] = $row['partyName'];
	  $k++;
	}
	//-----------------Process-----------------------------------
	 $r = 0;                                                                     
	 $select = "SELECT processId,processName                                    
	             FROM process";                                                   
	$selectResult = mysql_query($select);                                       
	while($row = mysql_fetch_array($selectResult))                              
	{                                                                           
	  $catprocessArray['processId'][$r]   = $row['processId'];                    
	  $catprocessArray['processName'][$r] = $row['processName'];                      
	  $r++;                                                                     
	}                                                                           
	  
	//---------------------material---------------------------
	  $m = 0;                                                                     
	  $select = "SELECT materialId,materialName                                    
	               FROM material
	              ORDER BY materialName";                                                   
	$selectResult = mysql_query($select);                                       
	while($row = mysql_fetch_array($selectResult))                              
	{                                                                           
	  $catmaterialArray['materialId'][$m]   = $row['materialId'];                    
	  $catmaterialArray['materialName'][$m] = $row['materialName'];                      
	  $m++;                                                                     
	}                    
	
	$incomingRegister = array();
	$i =0;
	$selectincomingRegister = "SELECT incomingId,partyName,DATE_FORMAT(incoming.IncomingDate,'%d-%m-%Y')AS IncomingDate,qty,challan,weight,processName1.processName AS processName1,
	                                  processName2.processName AS processName2,processName3.processName AS processName3,materialName,hardne
	                             FROM incoming
	                             LEFT JOIN party ON party.partyId = incoming.partyId
	                             LEFT JOIN process AS processName1 ON processName1.processId = incoming.processId1
	                             LEFT JOIN process AS processName2 ON processName2.processId = incoming.processId2
	                             LEFT JOIN process AS processName3 ON processName3.processId = incoming.processId3
	                             LEFT JOIN material ON material.materialId = incoming.materialId
	                            WHERE incoming.status = 'planning'   ";
	$selectincomingRegisterRes = mysql_query($selectincomingRegister);
	while($incomingRow = mysql_fetch_array($selectincomingRegisterRes))
	{
		$incomingRegister[$i]['incomingId']   = $incomingRow['incomingId'];
		$incomingRegister[$i]['partyName']    = $incomingRow['partyName'];
		$incomingRegister[$i]['IncomingDate'] = $incomingRow['IncomingDate'];
		$incomingRegister[$i]['qty']          = $incomingRow['qty'];
		$incomingRegister[$i]['challan']      = $incomingRow['challan'];
		$incomingRegister[$i]['weight']       = $incomingRow['weight'];
		$incomingRegister[$i]['processName1'] = $incomingRow['processName1'];
		$incomingRegister[$i]['processName2'] = $incomingRow['processName2'];
		$incomingRegister[$i]['processName3'] = $incomingRow['processName3'];
		$incomingRegister[$i]['material']     = $incomingRow['materialName'];
		$incomingRegister[$i]['hardne']       = $incomingRow['hardne'];
		$i++;
	}
}
include("./bottom.php");
$smarty->assign("printBtn",$printBtn);
$smarty->assign("catTitleArray",$catTitleArray);
$smarty->assign("catprocessArray",$catprocessArray);
$smarty->assign("catmaterialArray",$catmaterialArray);
$smarty->assign("incomingRegister",$incomingRegister);
$smarty->assign("incomeingMasterId",$incomeingMasterId);
$smarty->assign("msg",'<font size="5">No Record Found</font>');
$smarty->display('incomingRegister.tpl');
?>